<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of LastFM_User
 *
 * @author techi
 */
class LastFM_User
{

    /**
     *
     * @var bool
     */

    public $autoplay = true;

    /**
     *
     * @var string
     */
    private $_user;

    /**
     *
     * @param string $user
     */
    public function __construct($user)
    {
        $this->_user = $user;
    }

    /**
     *
     * @return string
     */
    public function getName()
    {
        return $this->_user;
    }

    /**
     *
     * @return string
     */
    public function __toString()
    {
        return $this->getName();
    }

    /**
     *
     * @return Zend_Rest_Client_Result
     */
    public function getFriends()
    {
        $fm = LastFM::getInstance();

        return $fm->getUserFriends($this->getName());
    }

    /**
     *
     * @return Zend_Rest_Client_Result
     */
    public function getInfo()
    {
        $fm = LastFM::getInstance();

        return $fm->getUserInfo($this->getName());
    }

    /**
     *
     * @return Zend_Rest_Client_Result
     */
    public function getTopArtists()
    {
        $fm = LastFM::getInstance();

        return $fm->getUserTopArtists($this->getName());
    }


    /**
     *
     * @param integer $limit
     * @return Zend_Rest_Client_Result
     */

    public function getLovedTracks($limit = null)
    {
        $fm = LastFM::getInstance();

        return $fm->getUserLovedTracks($this->getName(), $limit);
    }

    /**
     *
     * @param string $period
     * @return Zend_Rest_Client_Result
     */

    public function getTopTracks($period = null)
    {
        $fm = LastFM::getInstance();

        return $fm->getUserTopTracks($this->getName(), $period);
    }


    /**
     *
     * @var LastFM_User
     */
    private static $_activeUser;

    /**
     * Vraci instanci aktualne prihlaseneho uzivatele
     * Bud pres Zend_Auth nebo session
     * 
     * Pokud uzivatel nema identitu vyhodi se vyjimka
     *
     * @return LastFM_User
     */
    public static function getActiveUser()
    {
        $class = __CLASS__;

        if (!is_null(self::$_activeUser))
        {
            return self::$_activeUser;
        }

        if (Zend_Auth::getInstance()->hasIdentity())
        {
            $user = Zend_Auth::getInstance()->getIdentity();

            

            self::$_activeUser = new $class($user);
//            self::$_activeUser->autoplay = self::$_activeUser->hasAutoplay();
        }
        else
        {
            $session = new Zend_Session_Namespace('fm');

            if (!empty($session->fm))
            {
                self::$_activeUser = new $class($session->fm);
            }
        }

        if (is_null(self::$_activeUser))
        {
            throw new LastFM_Exception("No User identity");
        }

        return self::$_activeUser;
    }

    /**
     * Vraci true, pokud existuje identita uzivatele
     *
     * @return bool
     */
    public static function isActiveUser()
    {
        if (!is_null(self::$_activeUser))
        {
            return true;
        }

        if (Zend_Auth::getInstance()->hasIdentity())
        {
            return true;
        }

        $session = new Zend_Session_Namespace('fm');

        if (!empty($session->fm))
        {
            return true;
        }

        return false;
    }

}

